import axios from "axios"
import {
    upLoadImg
} from "@/api/index"
export default {
    //文件上传
    async posts() {
        return new Promise((resolve, reject) => {

            upLoadImg({
                class: 0,
            }).then((res) => {
                resolve(res)
            })
        })
    },
    base64ToArrayBuffer(base64) {
        var binary_string = window.atob(base64);
        var len = binary_string.length;
        var bytes = new Uint8Array(len);
        for (var i = 0; i < len; i++) {
            bytes[i] = binary_string.charCodeAt(i);
        }
        console.log(bytes.buffer);
        return bytes.buffer;
    },
    request(url, data) {
        console.log('url', url)
        // console.log('data', data)
        return new Promise((resolve, reject) => {
            axios({
                url: url,
                method: "PUT",
                header: {
                    'content-type': 'image/jpeg',
                },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: data,
                responseType: 'ArrayBuffer'
            }).then((res) => {
                console.log('res', res, )
                resolve(res)
            }).catch((e) => {
                console.log('e', e)
            })
        })
    },



    blobToBase64(file, type = "base64") {
        return new Promise((resolve) => {
            let fileRead = new FileReader();
            if (type === "base64") {
                fileRead.readAsDataURL(file);
            } else if (type === "buffer") {
                fileRead.readAsArrayBuffer(file);
            }
            fileRead.onload = (ev) => {
                resolve(ev.target.result);
            };
        });
    },




    async cosupdata(index, file) {
        var _this = this
        var base64 = {}
        var ArrayBufferImg
        _this.blobToBase64(file.file).then(response => {
            base64 = response.split(",")[1]
            ArrayBufferImg = this.base64ToArrayBuffer(base64)
        })
        let res = await _this.posts();
        if (res.err == 0) {
            // console.log(res.data.upload);
            let response = await _this.request(res.data.upload, ArrayBufferImg)
            // {"Content-Type":"image/jpeg",'Access-Control-Allow-Origin':'*'};
            console.log(response, "response")
            if (response.status == 200) {
                return res
            } else {
                console.log("9999999999999")
                return {
                    code: 10006,
                    'errmsg': '上传文件失败...'
                };
            }

        } else {
            return {
                code: 10009,
                'msg': res.msg
            };
        }


    },

    async upimg(callback, index, obj) {
        console.log(obj);
        var _this = this
        console.log("555555555555555555555")
        var data = await this.cosupdata(index || 0, obj)
        return callback(data)
    },
}